<?php  
namespace common\services;
use yii\helpers\StringHelper;

class SecurityHelper {

    public static function base64UrlEncode($input)
    {
        return strtr(base64_encode($input), '+/', '-_');
    }

    /**
     * Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
     *
     * @see https://tools.ietf.org/html/rfc4648#page-7
     * @param string $input encoded string.
     * @return string decoded string.
     * @since 2.0.12
     */
    public static function base64UrlDecode($input)
    {
        return base64_decode(strtr($input, '-_', '+/'));
    }

    /** 
     * 把aes-128-cbc加密的字符串解密 $string字符串的格式 key + iv + ciphertext
     * key 和iv 都是固定16位长度字符串
     * 把前端提交的数据解密
     * @param string $string 
     */
    public static function decode( $string ) 
    {
        $blockSize = 16;
        if (empty($string) || !is_string($string)) 
             return $string;
        if (StringHelper::byteLength($string) < $blockSize *2 )
             return $string;

        try{
            
            $cipher     = 'aes-128-cbc';
            $orign      = self::base64UrlDecode($string);
            $key        = StringHelper::byteSubstr($orign, 0, $blockSize);
            $iv         = StringHelper::byteSubstr($orign, $blockSize, $blockSize);
            $ciphertext = StringHelper::byteSubstr($orign, $blockSize *2  );
            $plantext   = openssl_decrypt($ciphertext, $cipher, $key, 0, $iv);
            if($plantext) 
                return $plantext;
            else 
                return $string;

        }catch(\Exception $e){

            return $string;
        }
        
    }
    /** 
     * 采用aes-128-cbc 加密字符串
     * 
     * @param string $plaintext 待加密的明文
     */
    public static function encode($plaintext)
    {
        try{
            $secret = 'eydbktGMqBYUCK80';
            $cipher = 'aes-128-cbc';
            $keySize = 16;
            $iv = \Yii::$app->getSecurity()->generateRandomKey($keySize);

            $ciphertext = openssl_encrypt($plaintext, $cipher, $secret, 0, $iv);

            return self::base64UrlEncode( $iv.$ciphertext );

        }catch(\Exception $e){

            return $plaintext;
        }
    }
    /** 
     * encode 函数的逆算法，解密
     * @param string $string
     */
    public static function decode_by_key($string)
    {
     
        try{
            $blockSize = 16; 
            $cipher = 'aes-128-cbc';
            $orign  = self::base64UrlDecode($string);
            $secret = 'eydbktGMqBYUCK80'; 
            $iv     =  StringHelper::byteSubstr($orign, 0, $blockSize);
            $ciphertext = StringHelper::byteSubstr($orign, $blockSize   );
            $plantext =   openssl_decrypt($ciphertext, $cipher, $secret, 0, $iv);
            if($plantext) 
                return $plantext;
            else 
                return $string;

        }catch(\Exception $e){

            return $string;
        }   
    }
    /**
     * 加密一个数组里面的值后 转成json 给蜜糖接口发送
     * @param array $params
     * @return string
     */
    public static function array_encode ($params = [])
    { 
        foreach($params as $key=> $val){
            if(!empty($val))
                $params[$key] =  self::encode($val );               
        } 
        return  json_encode($params) ;
    }
 
}